﻿using ChoJu.GlobalInfo;
using ChoJu.ModelManager.Config;
using ChoJu.Single.DAL;
using ChoJuRating.UcControl;
using ChoJuRating.View;
using ChongJu.EventManager;
using ChongJu.GlobalInfo;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ChoJu.Single.Views
{
    /// <summary>
    /// WeightView.xaml 的交互逻辑
    /// </summary>
    public partial class WeightView : UserControl
    {
        private WeightConfigModel _config;

        public WeightView()
        {
            InitializeComponent();

            _config =  LiteDBClient.Instance.Get<WeightConfigModel>(ConfigType.Weight);
            List<string> ports = SerialPort.GetPortNames().ToList();
            cboxSerports.ItemsSource = ports;
            cboxSerports.SelectedValue = _config.ComName;

            cboxBaudRates.ItemsSource = new List<int> {9600, 19200, 38400, 57600, 115200, 500000 };
            cboxBaudRates.SelectedValue = _config.BaudRate;

            tbTm.Text = _config.Interval.ToString();
            tbChange.Text = _config.Change.ToString();
            tbCount.Text = _config.Count.ToString();

            cboxType.ItemsSource = new List<string> { "卡索", "板子", "黑盒子" };
            cboxType.SelectedIndex = _config.WeiType - 1;
            GlobalVar.GlobalParamer.WeightType = _config.WeiType;

            btnZore.IsEnabled = false;
            btnWeight.IsEnabled = false;
            btnPleed.IsEnabled = false;
            if (_config.WeiType != 1)
            {
                SerialPortDal.Instance.SetRead(true);
            }


            Task.Run(() => GetWeight());
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            _config.Interval = int.Parse(tbTm.Text);
            _config.Count = (int)double.Parse(tbCount.Text);
            _config.Change = float.Parse(tbChange.Text);
            _config.WeiType = cboxType.SelectedIndex + 1;

            _config.ComName = cboxSerports.SelectedValue?.ToString();
            _config.BaudRate = (int)cboxBaudRates.SelectedValue;

            if(_config.Interval < 30)
            {
                CJMsgBox.ShowDialog("最小间隔30毫秒");
                return;
            }

            if(_config.Count < 1)
            {
                CJMsgBox.ShowDialog("最小采样个数为1");
                return;
            }

            LiteDBClient.Instance.Set(ConfigType.Weight, _config);

            if (GlobalVar.GlobalParamer.WeightType == 1)
                WeightDal.Instance.Init(_config);
            else
                SerialPortDal.Instance.Init(_config);
        }

        private void tbTm_GotFocus(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
                new Action(() =>
                {
                    KeyboardView kb = new KeyboardView(((TextBox)sender).Text);
                    if (kb.ShowDialog() == false)
                        return;

                    ((TextBox)sender).Text = kb.Result;
                }));
        }

        private void btnPleed_Click(object sender, RoutedEventArgs e)
        {
            SerialPortDal.Instance.Pleed();
            tbkCurrentWeight.Text = SerialPortDal.Instance.GetWeight().ToString();
        }

        private void btnZore_Click(object sender, RoutedEventArgs e)
        {
            SerialPortDal.Instance.Zero();
            tbkCurrentWeight.Text = SerialPortDal.Instance.GetWeight().ToString();
        }

        private void btnWeight_Click(object sender, RoutedEventArgs e)
        {
            var bol = int.TryParse(tboxWei.Text, out var value);
            if(bol == false)
            {
                CJMsgBox.ShowDialog("请输入正确的重量信息");
                return;
            }

            SerialPortDal.Instance.WeightWrite(value);
            tbkCurrentWeight.Text = SerialPortDal.Instance.GetWeight().ToString();
        }

        private void btnLink_Click(object sender, RoutedEventArgs e)
        {
            SerialPortDal.Instance.Init(_config);
        }

        private void GetWeight()
        {
            var wei = GlobalVar.GlobalParamer.WeightType != 1 ? SerialPortDal.Instance.GetCurrentWeight() : WeightDal.Instance.GetCurrentWeight();
            Dispatcher.Invoke(() => tbkCurrentWeight.Text = wei.ToString());

            Thread.Sleep(500);
            GetWeight();
        }

        private void cboxType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cbox = sender as ComboBox;
            switch(cbox.SelectedIndex)
            {
                case 0: GlobalVar.GlobalParamer.WeightType = 1; _config.WeiType = 1; WeightDal.Instance.Init(_config);  break;
                case 1: GlobalVar.GlobalParamer.WeightType = 2; _config.WeiType = 2; SerialPortDal.Instance.Init(_config); break;
                case 2: GlobalVar.GlobalParamer.WeightType = 3; _config.WeiType = 3; SerialPortDal.Instance.Init(_config); break;
            }
        }

        private void btnSE_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if(btn.Content.ToString() == "暂停")
            {
                btn.Content = "开始";
                SerialPortDal.Instance.SetRead(false);
                btnZore.IsEnabled = true;
                btnWeight.IsEnabled = true;
                btnPleed.IsEnabled = true;
            }
            else
            {
                btn.Content = "暂停";
                SerialPortDal.Instance.SetRead(true);
                btnZore.IsEnabled = false;
                btnWeight.IsEnabled = false;
                btnPleed.IsEnabled = false;
            }
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,
                new Action(() =>
                {
                    KeyboardView kb = new KeyboardView(((TextBox)sender).Text);
                    if (kb.ShowDialog() == false)
                        return;

                    ((TextBox)sender).Text = kb.Result;
                }));
        }
    }
}
